package org.example;



import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class JsonManager {

    public static JSONObject managerJson(List<JSONObject> fieldInfos, String jsonData) {
        JSONObject inputJson = JSONObject.parseObject(jsonData);
        JSONObject outputJson = new JSONObject();
        List<String> answer = new ArrayList<>();

        for (JSONObject fieldInfo : fieldInfos) {
            String fieldName = fieldInfo.getString("field_name");
            String qaType = fieldInfo.getString("qa_type");
            String fieldType = fieldInfo.getString("field_type");
            String field = fieldInfo.getString("field");

            Object value = inputJson.get(field);
            switch (qaType) {
                case "question":
                    if (value != null) {
                        outputJson.put(qaType, fieldName + ":" + value);
                    }
                    break;
                case "answer":
                    if (value != null) {
                        String answerKey = fieldName + ":";
                        if (fieldType.equals("array")) {
                            JSONArray answerArray = new JSONArray();
                            for (Object obj : (JSONArray) value) {
                                JSONObject answerObj = JSONObject.parseObject(obj.toString());
                                String answerItem = buildAnswer(answerObj, fieldInfo.getJSONArray("children"));
                                answerArray.add(answerItem);
                            }
                            answer.add(answerKey + "[" + StringUtils.join(answerArray, ";") + "]");
                            outputJson.put(qaType, StringUtils.join(answer, ","));
                        } else if (fieldType.equals("object")) {
                            JSONObject answerObj = (JSONObject) value;
                            String answerItem = buildAnswer(answerObj, fieldInfo.getJSONArray("children"));
                            answer.add(answerKey + "[" + answerItem + "]");
                            outputJson.put(qaType, StringUtils.join(answer, ","));
                        }
                    }
                    break;
                case "key_point":
                    String keyPoint = fieldName + ":";
                    if (value == null) {
                        outputJson.put(qaType, "");
                    } else {
                        if (fieldType.equals("object")) {
                            JSONArray keyPointArray = new JSONArray();
                            for (Object obj : (JSONArray) value) {
                                JSONObject answerObj = JSONObject.parseObject(obj.toString());
                                String answerItem = buildAnswer(answerObj, fieldInfo.getJSONArray("children"));
                                keyPointArray.add(answerItem);
                            }
                            outputJson.put(qaType, keyPoint + "[" + keyPointArray.toString() + "]");
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return outputJson;
    }

    private static String buildAnswer(JSONObject answerObj, JSONArray children) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            JSONObject child = children.getJSONObject(i);
            String childField = child.getString("field");
            String childFieldType = child.getString("field_type");
            Object childValue = answerObj.get(childField);
            if (childValue != null) {
                if ("object".equals(childFieldType)) {
                    //递归遍历
                    String answerItem = buildAnswer(answerObj.getJSONObject(childField), child.getJSONArray("children"));
                    sb.append(child.getString("field_name")).append(":[").append(answerItem).append(",]");
                } else {
                    sb.append(child.getString("field_name")).append(":").append(childValue);
                }
                if (i < children.size() - 1) {
                    sb.append(",");
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        // 模拟参数-字段信息
        String field = "[{\"field\":\"ques\",\"field_name\":\"测试问题\",\"field_type\":\"string\",\"qa_type\":\"question\"},{\"field\":\"ans\",\"field_name\":\"测试答案\",\"field_type\":\"array\",\"qa_type\":\"answer\",\"children_type\":\"object\",\"children\":[{\"field\":\"E\",\"field_name\":\"EEE\",\"field_type\":\"string\",\"qa_type\":\"\"},{\"field\":\"F\",\"field_name\":\"FFF\",\"field_type\":\"string\",\"qa_type\":\"\"}]},{\"field\":\"ansB\",\"field_name\":\"测试答案B\",\"field_type\":\"object\",\"qa_type\":\"answer\",\"children\":[{\"field\":\"G\",\"field_name\":\"GGG\",\"field_type\":\"object\",\"qa_type\":\"\",\"children\":[{\"field\":\"H\",\"field_name\":\"HHH\",\"field_type\":\"string\",\"qa_type\":\"\"}]}]},{\"field\":\"keyp\",\"field_name\":\"测试关键字段\",\"field_type\":\"object\",\"qa_type\":\"key_point\",\"children\":[{\"field\":\"K\",\"field_name\":\"KKK\",\"field_type\":\"string\",\"qa_type\":\"\"}]}]";
        List<JSONObject> fieldInfos = JSONArray.parseArray(field, JSONObject.class);

        // 模拟参数-JSON数据
        String jsonData = "{\"ques\":\"今天几号？\",\"ans\":[{\"E\":\"31\",\"F\":\"10.31\"},{\"E\":\"31号\",\"F\":\"10月31号\"}],\"ansB\":{\"G\":{\"H\":\"10.30\"}}}";

        // 调用
        JSONObject result = managerJson(fieldInfos, jsonData);
        System.out.println(result.toJSONString());
    }
}
